package edu.base.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import edu.base.config.CmsConfig;
import edu.base.dao.UserMapper;
import edu.base.model.Users;
import edu.base.service.UserService;
import edu.base.util.Result;
import edu.base.util.ResultCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpSession;
import java.io.Console;
import java.io.File;
import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    UserMapper userMapper;
    @Autowired
    UserService userService;


    /*手机验证码登录*/
    @PostMapping("/loginphone")
    public Result loginph(String code, HttpSession session) {
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("tel",session.getAttribute("memPhone"));
        Users user = userMapper.selectOne(wrapper);
        if(user!=null){
            if(code.equals(session.getAttribute("code"))){
                session.removeAttribute("code");
                session.setAttribute("user_id",user.getUserId());
                return Result.success(user);
            }else{
                return Result.failure(ResultCode.LOGIN_CODE_ERROR);
            }

        }
        return Result.failure(ResultCode.USER_NOT_FOUND);
    }

    /*手机号/用户名登录*/
    @PostMapping("/loginuser")
    public Result<Users> login(String content,String pwd,HttpSession session){
        Result<Users> result = userService.loginUser(content,pwd);
        if(result.getCode()==200){
            session.setAttribute("user_id",result.getData().getUserId());
        }
        return result;
    }

    /*找回密码*/
    /*找回密码
     * 输入身份验证，
     * 输入手机号发送验证码
     * 修改密码*/

    @PostMapping("/findpwd")
    public Result findpwd(String pwd,String code,HttpSession session){
        System.out.println(session.getAttribute("code"));
        String phone = session.getAttribute("memPhone").toString();
        if(code.equals(session.getAttribute("code").toString())){
            userService.updatePassword(pwd,phone);
        }else {
            return Result.failure(ResultCode.LOGIN_CODE_ERROR);
        }
        return Result.success();
    }


}
